package com.xgggh.redpack.ui.home;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.DividerItemDecoration;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseFragment;
import com.xgggh.redpack.databinding.HomeFragmentBinding;
import com.xgggh.redpack.databinding.RankFragmentBinding;
import com.xgggh.redpack.domain.activity.ActivityItem;
import com.xgggh.redpack.domain.rank.Rank;

import com.xgggh.redpack.ui.home.viewbinder.RankViewBinder;

import java.util.List;

import io.reactivex.subscribers.DisposableSubscriber;

public class RankFragment extends BaseFragment {

    RankFragmentBinding mBinding;

    public static RankFragment newInstance() {
        return new RankFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.rank_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.rank, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter.register(Rank.class,new RankViewBinder());

        adapter.setItems(items);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        mBinding.twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                //getRankList(firstPage(),size,sort);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                //getRankList(nextPage(),size,sort);
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && items.size()==0){
           // getRankList(firstPage(),size,sort);
        }
    }

    /**
     * @title 排名列表
     * @param page
     * @param size
     * @param sort
     */
   /* private  void getRankList(final int page,int size,String sort) {
        new MockRankService(getBaseActivity().getMockDelegate(RankService.class))
                .getRankList(page, size, sort)
                .compose(getBaseActivity().ioTransformer())
                .subscribe(new DisposableSubscriber<RankListRsp>() {
                    @Override
                    public void onNext(RankListRsp rsp) {
                        List<Rank> itemList = rsp.getData();
                        if (page == firstPage()) {
                            if (itemList == null || itemList.size() == 0) {
                                // 没有数据，显示空界面，点击跳转发红包界面
                                return;
                            }
                            items.clear();
                            items.addAll(itemList);
                            adapter.notifyDataSetChanged();
                        } else if (page == nextPage()) {
                            if (itemList == null || itemList.size() == 0) {
                                Toast.makeText(getBaseActivity(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            items.addAll(itemList);
                            adapter.notifyDataSetChanged();
                        }

                        if (itemList != null && itemList.size() != 0) {
                            setCurrentPage(page);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        t.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        if (page == firstPage()) {
                            mBinding.twinklingRefreshLayout.finishRefreshing();
                        } else {
                            mBinding.twinklingRefreshLayout.finishLoadmore();
                        }
                    }
                });
    }*/
}
